package com.dunjx.oauth.config;

import com.dunjx.user.feign.UserFeign;
import com.dunjx.oauth.uitl.UserJwt;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collection;


/*****
 * 自定义授权认证类
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    ClientDetailsService clientDetailsService;

    @Autowired
    UserFeign userFeign;


    /****
     * 自定义授权认证
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //=============================(客户端认证)客户端信息认证 start======================
        //取出身份，如果身份为空说明没有认证
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //没有认证统一采用httpbasic认证，httpbasic中存储了client_id和client_secret，开始认证client_id和client_secret
        if (authentication == null) {
            ClientDetails clientDetails = null;
            try {
                System.err.println("客户端id:"+username);
                clientDetails = clientDetailsService.loadClientByClientId(username);
            } catch (ClientRegistrationException e) {
                e.printStackTrace();
            }
            String clientSecret1 = clientDetails.getClientSecret();
            System.out.println("数据库存储的客户端秘钥为:"+clientSecret1);
            if (clientDetails != null) {
                //秘钥
                String clientSecret = clientDetails.getClientSecret();
                //数据库查找方式
                return new User(username,clientSecret, AuthorityUtils.commaSeparatedStringToAuthorityList(""));
            }
        }
        //=============================客户端认证 end======================


        //=============================(密码模式走这个) 用户信息认证 end======================


        if (StringUtils.isEmpty(username)) {
            return null;
        }
        System.err.println("用户名为:"+username);
        Result userResult = userFeign.findByUsername(username);
        System.err.println("userResult"+userResult);
        if (userResult.getData()==null||userResult==null){
            return null;
        }
        com.dunjx.user.pojo.User userInfo = (com.dunjx.user.pojo.User) userResult.getData();
        String password = userInfo.getPassword();

        String permissions = "ROLE_user,ROLE_vip,admin";
        UserJwt userDetails = new UserJwt(username,password,AuthorityUtils.commaSeparatedStringToAuthorityList(permissions));

        return userDetails;
        //=============================用户信息认证 end======================
    }
}
